package com.xiaoxu.rental.controller;
import com.xiaoxu.rental.entity.Permission;
import com.xiaoxu.rental.service.PermissionService;
import com.xiaoxu.rental.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**

 * @author xiaoxu
 * @since 2024-07-24
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping("/rental/permission")
@SuppressWarnings("all")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    @ApiOperation(value = "根据用户id获取权限列表")
    @GetMapping("/getAuthorizedPermissions/{useId}")
    public Result<List<Permission>> getAuthorizedPermissions(@PathVariable("userId") Integer userId){
        List<Permission> permissionList = permissionService.selectPermissionListByUserId(userId);
        return Result.success(permissionList);

    }

    @ApiOperation(value = "获取权限列表")
    @GetMapping
    public Result list() {
        return Result.success(permissionService.selectList());
    }

    @ApiOperation(value = "获取权限树")
    @GetMapping("/tree")
    public Result tree() {
        return Result.success(permissionService.selectTree());
    }

    @ApiOperation(value = "新增权限")
    @PostMapping("/save")
    public Result save(@RequestBody Permission permission) {
        Integer permissionType = permission.getPermissionType();
        if (permissionType != 2) {
            String routeUrl = permission.getRouteUrl();
            if (routeUrl.contains("/")) {
                permission.setRouteName(routeUrl
                        .substring(routeUrl.lastIndexOf("/") + 1));
                permission.setRoutePath(routeUrl.substring(routeUrl.lastIndexOf("/")));

            }
        }
        return permissionService.save(permission) ? Result.success() :
                Result.fail();
    }

    @ApiOperation(value = "更新权限")
    @PutMapping("/update")
    public Result update(@RequestBody Permission permission){

        Integer permissionType = permission.getPermissionType();
        if (permissionType != 2) {
            String routeUrl = permission.getRouteUrl();//
            if (routeUrl.contains("/")) {
                permission.setRouteName(routeUrl
                        .substring(routeUrl.lastIndexOf("/") + 1));
                permission.setRoutePath(routeUrl.substring(routeUrl.lastIndexOf("/")));

            }
        }
        return permissionService.updateById(permission) ? Result.success() :
                Result.fail();
    }

    @ApiOperation(value = "删除权限")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        return permissionService.removeById(id) ? Result.success() :
                Result.fail();
    }

    @ApiOperation(value = "判断是否有子节点")
    @GetMapping("hasChildren/{id}")
    public Result hasChildren(@PathVariable Integer id){
        return permissionService.hasChildren(id) ?
                Result.success().setMessage("有") :
                Result.success().setMessage("无");
    }

}

